using System;
using System.Data;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Data.Security;
using BLL = Auto.Data.Systems.BLL;
using Model = Auto.Data.Systems.Model;
using Auto.Web.Common;
using Auto.Components;
using System.Reflection;

namespace BaseSystem._System.UserControl
{
    public partial class ucRoleMenu : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// Gets the sys roles sys role func list.
        /// </summary>
        /// <returns></returns>
        private List<SysRoles.SysRoleFunc> GetSysRolesSysRoleFuncList()
        {
            List<SysRoles.SysRoleFunc> listRoles = new List<SysRoles.SysRoleFunc>();
            SysRoles.SysRoleFunc sysRoleFunc = new SysRoles.SysRoleFunc();
            foreach (KeyValuePair<string, string> checkedNode in this.UcMenutree1.CheckedFuncNodes)
            {
                sysRoleFunc = new SysRoles.SysRoleFunc();
                sysRoleFunc.FuncID = checkedNode.Key;
                sysRoleFunc.RoleID = txtRoleID.Text;
                sysRoleFunc.SysID = lblSysID.Text;
                listRoles.Add(sysRoleFunc);
            }
            return listRoles;
        }

        /// <summary>
        /// Gets the sys roles sys role func list.
        /// </summary>
        /// <returns></returns>
        private List<SysRoleFuncOperate> GetSysRoleFuncOperates()
        {
            List<SysRoleFuncOperate> listOperates = new List<SysRoleFuncOperate>();
            SysRoleFuncOperate sysRoleFuncOperate = null;
            foreach (KeyValuePair<string, string> checkedNode in this.UcMenutree1.CheckedOperateNodes)
            {
                string[] funcOperate = checkedNode.Key.Split('|');
                sysRoleFuncOperate = new SysRoleFuncOperate();
                sysRoleFuncOperate.FuncID = funcOperate[0];
                sysRoleFuncOperate.RoleID = txtRoleID.Text;
                sysRoleFuncOperate.SysID = lblSysID.Text;
                sysRoleFuncOperate.OperateID = funcOperate[1];
                sysRoleFuncOperate.Note = checkedNode.Value;
                listOperates.Add(sysRoleFuncOperate);

            }
            return listOperates;
        }

        protected void lkbtnOK_Click(object sender, EventArgs e)
        {
            SysRoles sysRoles = new SysRoles();

            sysRoles.Note = this.txtNote.Text;
            sysRoles.Pause = chkPause.Checked;
            sysRoles.RoleName = this.txtRoleName.Text;
            sysRoles.SysID = lblSysID.Text;
            sysRoles.RoleID = txtRoleID.Text;
            sysRoles.SysRoleFuncs = GetSysRolesSysRoleFuncList();
            sysRoles.SysRoleFuncOperates = GetSysRoleFuncOperates();
            if (this.txtRoleID.ReadOnly)
            {
                try
                {
                    sysRoles.Update();
                    BindGridView();
                }
                catch (Exception ex)
                {
                    Jscript.AjaxAlert(this.Page, ex.Message);
                }
            }
            else
            {
                if (!sysRoles.Exists(sysRoles.SysID, sysRoles.RoleID))
                {
                    try
                    {
                        sysRoles.Insert();
                        BindGridView();
                    }
                    catch (Exception ex)
                    {
                        Jscript.AjaxAlert(this.Page, ex.Message);
                    }
                }
                else
                {

                }
            }
            this.Visible = false;
        }

        private void BindGridView()
        {
            Page p = Parent.Page;
            p.Page.GetType().GetMethod("BindGridView").Invoke(p, new object[0]);
        }

        protected void lkbtnCancel_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        public void New(string sysID, string sysName)
        {
            lblSysID.Text = sysID;
            txtRoleID.Text = txtRoleName.Text = txtNote.Text = string.Empty;
            txtRoleID.ReadOnly = false;
            this.UcMenutree1.TreeInit(sysID, sysName);
        }

        public void Display(SysRoles sysRoles, string sysName)
        {
            if (sysRoles != null)
            {
                lblSysID.Text = sysRoles.SysID;
                txtRoleID.Text = sysRoles.RoleID.ToString();
                txtRoleName.Text = sysRoles.RoleName;
                txtNote.Text = sysRoles.Note;
                txtRoleID.ReadOnly = true;
                this.UcMenutree1.TreeInit(lblSysID.Text, sysName);
                if (sysRoles.SysRoleFuncs != null && sysRoles.SysRoleFuncs.Count > 0)
                {
                    IDictionary<string, string> checkedNode = null;
                    checkedNode = new Dictionary<string, string>();

                    SysRoleFuncOperate sysRoleFuncOperate = new SysRoleFuncOperate();

                    IDictionary<string, string> checkedOperateNode = new Dictionary<string, string>();
                    if (sysRoles.SysRoleFuncs != null && sysRoles.SysRoleFuncs.Count > 0)
                    {
                        foreach (SysRoles.SysRoleFunc roleFunc in sysRoles.SysRoleFuncs)
                        {
                            checkedNode.Add(roleFunc.RecID.ToString(), roleFunc.FuncID);
                        }
                    }
                    if (sysRoles.SysRoleFuncOperates != null && sysRoles.SysRoleFuncOperates.Count > 0)
                    {
                        foreach (SysRoleFuncOperate operate in sysRoles.SysRoleFuncOperates)
                        {
                            checkedOperateNode.Add(operate.FuncID + "|" + operate.OperateID, operate.OperateID);
                        }
                    }
                    this.UcMenutree1.CheckedNodes = checkedNode;
                    this.UcMenutree1.CheckedOperateNodes = checkedOperateNode;
                }
            }
        }
    }
}